// pages/personal-info/personal-info.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
const QQMapWX = require('../../sdk/qqmap-wx-jssdk.min.js');
import { getMemberInfo, editStaffInfo } from '../../static/api/api.js'
let cropperObj;
//获取应用实例
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curDate:null,
        personal:{
            head_img:'',
            real_name:'',
            birthday:'',
            sex:''
        },
        
        sexArr:[
            {
                name:'男',
                id:"1"
            },
            {
                name: '女',
                id: "2"
            }
        ],
        cropperParams: {
            width: 350,
            height: 350,
            src: ''
        },
        isShowCropper: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'curDate': utils.formatDate(new Date(),'yyyy-MM-dd')
        })
        cropperObj = this.selectComponent("#imageCropper");
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    inputValueChange(e) {
        utils.inputValueChange(e, this)
    },
    chooseSexPop(){
        let itemArr = [];
        let that = this;
        this.data.sexArr.forEach((el, index) => {
            itemArr[index] = el.name;
        })
        wx.showActionSheet({
            itemList: itemArr,
            success: function (res) {
                console.log(res.tapIndex)
                that.setData({
                    'personal.sex': that.data.sexArr[res.tapIndex].id
                })
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        })
    },
    //  点击日期组件确定事件  
    bindDateChange: function (e) {
        console.log('选择的值：', e.detail.value)
        var month = e.detail.value[0];
        var days = e.detail.value[1];
        var text = this.data.range[0][month] + this.data.range[1][days]
        this.setData({
            dates: text
        })
    },
    changeDate(res){
        console.log(res)
        this.setData({
            'personal.birthday':res.detail.value
        })
    },
    getUserInfo() {
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        getMemberInfo().then(response => {
            wx.hideLoading();
            this.setData({
                'personal.real_name': response.data.real_name,
                'personal.sex': response.data.sex,
                'personal.head_img': response.data.head_img,
                'personal.phone': response.data.phone
            })
        }).catch(response => {
            wx.hideLoading();
        })
    },
    updateMemberInfo() {
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        editStaffInfo(this.data.personal).then(response => {
            wx.hideLoading();
            wx.showToast({
                title: '修改成功',
            })
        }).catch(response => {
            wx.hideLoading();
        })
    },
    chooseImage(e) {
        let that = this;
        cropperObj = null
        cropperObj = this.selectComponent("#imageCropper");
        cropperObj.upload(function () {
            that.setData({
                'isShowCropper': true,
            })
        }, function () {
            that.setData({
                'isShowCropper': false,
            })
        });
        this.loadimage()
        that.setData({
            uploadType: 1
        })
    },
    clipCropperImg() {
        let that = this;
        console.log('裁剪的图片');
        wx.showLoading({
            title: '图片处理中...',
        })
        cropperObj.getImg(function (res) {
            wx.hideLoading()
            that.uploadImg(res.url, function (data) {
                console.log(data)
                that.setData({
                    'personal.head_img': data.data.url,
                    'isShowCropper': false
                })

            })
        })
    },
    cropperload(e) {
        console.log("cropper初始化完成");
    },
    loadimage(e) {
        cropperObj.imgReset();
    },
    //上传图片
    uploadImg(path, cb) {
        let that = this;
        setTimeout(function () {
            var urls = config.interfaceHost + "/api.php/common/Qiniu/upLoadImg";
            console.log(path);
            wx.showLoading({
                title: '加载中..',
            })
            wx.uploadFile({
                url: urls,
                filePath: path,
                name: 'image',
                header: {
                    'content-type': 'multipart/form-data',
                    'site': config.site,
                    'os': config.os,
                    'token': wx.getStorageSync('token')
                },
                success: function (res) {
                    wx.hideLoading()
                    var str = res.data;
                    console.log(res.data);
                    if (typeof cb == 'function') {
                        cb(JSON.parse(res.data));
                    }

                },
                fail: function (res) {
                    wx.hideLoading()
                    console.log(res);
                    wx.showModal({
                        title: '提示',
                        content: "网络请求失败，请确保网络是否正常",
                        showCancel: false,
                        success: function (res) {

                        }
                    });
                    wx.hideToast();
                }
            });
        }, 500)
    },
    cancelClipBtn() {
        this.setData({
            'isShowCropper': false,
        })
        this.loadimage()
    },
})